<?php
defined('IN_LK') or die('Access Denied');
lk::lo('model', '', 0);
class content_model extends model
{
	public $objs = null;
	public $pxfs = null;
	
	public function __construct()
	{
		$this->tablename = 'content';
		parent::__construct();
		$this->objs = array(1 => '小学家教', 
							2 => '初中家教', 
							3 => '中考家教', 
							4 => '高中家教', 
							5 => '高考冲刺', 
							6 => '素质/特长辅导', 
							7 => '小升初家教', 
							8 => '奥数');
		$this->pxfs = array(1 => '辅导班',
							2 => '一对一',
							3 => '大学生家教',
							4 => '外教',
							5 => '陪读');
	}
	
	/**
	 * 热门点击
	 */
	public function get_hot($num)
	{
		return $this->get('id,title', "`status`='1'", "`hits` DESC", '', $num);
	}
	
	/**
	 * 推荐信息
	 */
	public function get_jinghua($num)
	{
		return $this->get('id,title', "`status`='1' AND `posids` LIKE '%3%'", "`listorder` DESC", '', $num);
	}
	
	/**
	 * 公告
	 */
	public function get_announ($num)
	{
		return $this->get('id,title', "`catid`='3' AND `posids` LIKE '%4%' AND `status`='1'", '`id` DESC', '', $num);
	}
	
	/**
	 * 取标题
	 */
	public function get_title($title)
	{
		return $this->get_one('*', "`title`='$title' AND `status`='1'");
	}
	
	/**
	 * 根据id 取标题
	 */
	public function id2title($id)
	{
		$r = $this->get_one('title', "`id`='$id' AND `status`='1' AND `allow_comment`='1'");
		return $r['title'];
	}
	
	/**
	 * 取内容
	 * @param  int $id 内容id
	 * @return mixed {有内容:数组,无内容:false}
	 */
	public function is_exists($id)
	{
		return $this->get_one('*', "`id`='$id' AND `status`='1'");
	}
	
	/**
	 * 判断是否是某用户的信息
	 * @param int $userid 用户id
	 * @param int $id     信息id
	 * @return boolean {是:true, 否:false}
	 */
	public function is_myinfo($userid, $id)
	{
		return (boolean)$this->get_one('id', "`id`='$id' AND `userid`='$userid' AND `status`='1'");
	}
	
	/**
	 * 删除一条信息
	 */
	public function del_info($id)
	{
		return $this->update(array('status'=>0), "`id`='$id'");
	}
	
	/**
	 * 日最大发布数限制 包括删除的
	 * @return boolean {限制了:false, 未限制:true}
	 */
	public function not_allowpost()
	{
		$max = 0;
		$gruopid = $_SESSION['_groupid'];
		$userid  = $_SESSION['_userid'];
		$groups  = get_cache('grouplist');
		foreach($groups as $g)
		{
			if($g['groupid'] == $gruopid)
			{
				$max = $g['maxposts'];
				break;
			}
		}
		if($max)
		{
			$from = strtotime('today');
			$to   = strtotime('tomorrow');
			$res  = $this->_count("`userid`='$userid' AND `inputtime`<'$to' AND `inputtime`>'$from'");
			return $res <=$max;
		}
		return false;
	}
	
	/**
	 * 浏览次数加一
	 * 不加限制
	 */
	public function hits_count($id)
	{
		return $this->update(array('hits' => '+=1'), "`id`='$id'");
	}
}